<?php

namespace App\Service\Index\v1_2_2;

use App\Constants\ErrorCode;
use App\Helpers\ConfigUtil;
use App\Helpers\RedisUtil;
use App\Model\CallBase;
use App\Model\Setting\SettingUserLevel;
use App\Model\UserCoin;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Model\VoiceCall;
use App\Model\VideoCall;
use App\Service\CallBaseService;

/**
 * 速配呼叫
 */
class MatchCallService extends CallBaseService
{
    protected function RoVerifyList()
    {
        return [
            'call_type' => 'required|string'
        ];
    }

    public function handle()
    {
        $callType = $this->request->input('call_type', VideoCall::CALL_TYPE);
        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;

//        //是否实名认证
//        if (!UserProfilePersonal::isFaceVerified(UserProfilePersonal::getProfile($this->userId))) {
//            return $this->setError(ErrorCode::NO_FACE_VRFY, '平台提倡真实交友，必须完成实名认证后才能进行速配哦~');
//        }

        //真人认证
        if(!UserProfile::checkAvatarVrfied($this->userId)){
            return $this->setError(ErrorCode::REAL_AVATAR_NOT_VRFY, '平台提倡真实交友，必须完成真人认证后才能进行速配哦~');
        }

        //没有绑定手机
        $user = $this->getUser();
        if (!$user->phone) {
            return $this->setError(ErrorCode::PHONE_NOT_BIND, '平台提倡真实交友，必须绑定手机后才能进行速配哦~');
        }

        //是否等级达到
        $levelLmt = 1;
        if (SettingUserLevel::getUserLevel(UserProfile::getProfile($this->userId)) < $levelLmt) {
            return $this->setError(ErrorCode::USER_LEVEL_LMT, "您当前的魅力等级＜{$levelLmt}级，暂时无法参与速配，请先提升魅力等级后再参与速配~");
        }

        //首次弹出规则
        $callRule = '系统将为您接通新注册用户
        如果对方话单结束后的1小时内进行充值，对方充值金额的20%奖励将直接到您账户，并可提现。
        速配不足10s则视为通话不成功，不予充值分成。';
        if ($this->checkMatchRule()) {
            return $this->setError(ErrorCode::FIRST_SUPER_CHAT, '视频速配说明', [
                'tip' => $callRule,
            ]);
        }

        //天天福利
        $ret = $this->dailyFreeCall($this->userId, $callCls);
        $ret['tip'] = $callRule;
        return $ret;
    }

    /**
     * 是否显示
     */
    function checkMatchRule()
    {
        $key = "match_call_{$this->userId}";
        if (RedisUtil::get($key) == 1) return 0;
        RedisUtil::set($key, 1, 3600 * 240);
        return 1;
    }
}
